import pygame

pygame.init()

window = pygame.display.set_mode((400, 600))
pygame.display.set_caption('按钮点击效果')

window.fill((255, 255, 255))
pygame.display.flip()

font = pygame.font.Font('files/AlimamaDaoLiTi.ttf', 25)

# 确定按钮
bx1, by1, bw1, bh1 = 100, 200, 100, 50
pygame.draw.rect(window, (255, 155, 0), (bx1, by1, bw1, bh1))
text1 = font.render('确定', True, (255, 255, 255))
text1_w, text1_h = text1.get_size()
tx1 = bx1 + bw1 / 2 - text1_w / 2
ty1 = by1 + bh1 / 2 - text1_h / 2
window.blit(text1, (tx1, ty1))

# 取消按钮
bx2, by2 = 250, 200
pygame.draw.rect(window, (255, 0, 0), (bx2, by2, bw1, bh1))
text2 = font.render('取消', True, (255, 255, 255))
text2_w, text2_h = text2.get_size()
tx2 = bx2 + bw1 / 2 - text2_w / 2
ty2 = by2 + bh1 / 2 - text2_h / 2
window.blit(text2, (tx2, ty2))

pygame.display.update()

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

        # 鼠标按下
        if event.type == pygame.MOUSEBUTTONDOWN:
            mx, my = event.pos
            if bx1 <= mx <= bx1 + bw1 and by1 <= my <= by1 + bh1:
                pygame.draw.rect(window, (223, 225, 229), (bx1, by1, bw1, bh1))
                window.blit(text1, (tx1, ty1))
                pygame.display.update()


            elif bx2 <= mx <= bx2 + bw1 and by2 <= my <= by2 + bh1:
                pygame.draw.rect(window, (223, 225, 229), (bx2, by2, bw1, bh1))
                window.blit(text2, (tx2, ty2))
                pygame.display.update()


        if event.type == pygame.MOUSEBUTTONUP:
            # 确定按钮
            pygame.draw.rect(window, (255, 155, 0), (bx1, by1, bw1, bh1))
            window.blit(text1, (tx1, ty1))
            pygame.display.update()

            # 取消按钮
            pygame.draw.rect(window, (255, 0, 0), (bx2, by2, bw1, bh1))
            window.blit(text2, (tx2, ty2))
            pygame.display.update()